package com.example.john.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private EditText nameEt;
    private EditText ageEt;
    private ListView userLv;
    private UserAdapter adapter;
    private List<User> users=new ArrayList<User>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        nameEt=(EditText)findViewById(R.id.name_et);
        ageEt=(EditText)findViewById(R.id.age_et);
        userLv=(ListView)findViewById(R.id.users_listView);
        adapter=new UserAdapter(this,R.layout.user_layout,users);
        userLv.setAdapter(adapter);

    }

    public void getAllUsers(View view) {
        new Thread(){

            @Override
            public void run() {
                HttpURLConnection con=null;
                BufferedReader reader=null;
                try {
                    URL url=new URL("http://192.168.21.88:3000/users");
                    con=(HttpURLConnection)url.openConnection();

                    //getInputstream ,connect to net!!!!
                    InputStream in=con.getInputStream();
                    reader=new BufferedReader(new InputStreamReader(in));
                    StringBuilder builder=new StringBuilder();
                    String line=null;
                    while((line=reader.readLine())!=null){
                        builder.append(line);
                    }
                    String usersJson=builder.toString();
                    Log.i("john",usersJson);
                    //json parser.......
                    List<User> us= UserParser.parseUsers(usersJson);
                    users.clear();
                    users.addAll(us);
                    MainActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            adapter.notifyDataSetChanged();
                        }
                    });






                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    con.disconnect();

                }
            }
        }.start();
    }

    public void addUser(View view) {
        final String name=nameEt.getText().toString();
        final String age=ageEt.getText().toString();
        new Thread(){
            @Override
            public void run() {
                HttpURLConnection con=null;
                OutputStream out=null;
                BufferedReader reader=null;

                try {
                    URL url=new URL("http://192.168.21.88:3000/users");
                    con=(HttpURLConnection)url.openConnection();
                    con.setDoOutput(true);
                    con.setRequestMethod("POST");
                    con.addRequestProperty("Content-Type", "application/json");
                    out=con.getOutputStream();

                    String jsonString=null;

                    JSONObject jso=new JSONObject();
                    jso.put("name", name);
                    jso.put("age", age);
                    jsonString=jso.toString();

                    out.write(jsonString.getBytes());
                    //getInputstream ,connect to net!!!!
                    InputStream in=con.getInputStream();
                    reader=new BufferedReader(new InputStreamReader(in));
                    StringBuilder builder=new StringBuilder();
                    String line=null;
                    while((line=reader.readLine())!=null){
                        builder.append(line);
                    }
                    Log.i("john",builder.toString());
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        out.close();
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    con.disconnect();
                }


            }
        }.start();

    }
}
